package end.controller;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import end.bean.shiro.Resource;
import end.bean.shiro.Role;
import end.ser.ResourceSER;
import end.util.JsonUtil;
import end.util.ResourceTree;
import end.util.RightsHelper;

/**
 * 
 * ResourceForController 下午12:28:43 Copyright Zhaocj Inc. All right reserved.
 * Email : alldios@outlook.com
 * 
 */
@Controller
@RequestMapping("/resource")
public class ResourceForController {
	private final static Logger logger = LogManager
			.getLogger(AlbumForController.class);
	@Autowired
	public ResourceSER<Resource> resourceSER;

	public ResourceSER<Resource> getResourceSER() {
		return resourceSER;
	}

	public void setResourceSER(ResourceSER<Resource> resourceSER) {
		this.resourceSER = resourceSER;
	}

	// 查询所有的菜单资源
	@RequestMapping(value = "/queryMenuResourcesTree", method = { RequestMethod.POST })
	public @ResponseBody
	List<Object> queryMenuResourcesTree() throws Exception {
		ResourceTree rt = new ResourceTree();
		return rt.menuList(resourceSER.queryMenuResourcesTree());
	}

	// 查询所有的功能资源
	@RequestMapping(value = "/queryFuncResourcesTree", method = { RequestMethod.POST })
	public @ResponseBody
	List<Object> queryFuncResourcesTree() throws Exception {
		ResourceTree rt = new ResourceTree();
		return rt.menuList(resourceSER.queryFuncResourcesTree());
	}

	// 查询角色可用的菜单权限资源
	@RequestMapping(value = "/queryMenuResourcesTreeByRole", method = { RequestMethod.POST })
	public @ResponseBody
	List<Object> queryMenuResourcesTreeByRole(Role role) throws Exception {
		ResourceTree rt = new ResourceTree();
		Integer[] rightsID = resourceSER.queryResourceRightsByRoleID(role
				.getId());
		List<Resource> resourcesTree = resourceSER.queryMenuResourcesTree();
		for (Resource resource : resourcesTree) {
			resource.setChecked(RightsHelper.testRights(
					RightsHelper.sumRights(rightsID), resource.getId()));
		}
		return rt.menuList(resourcesTree);
	}

	// 查询焦角色可用的功能权限资源
	@RequestMapping(value = "/queryFuncResourcesTreeByRole", method = { RequestMethod.POST })
	public @ResponseBody
	List<Object> queryFuncResourcesTreeByRole(Role role) throws Exception {
		ResourceTree rt = new ResourceTree();
		Integer[] rightsID = resourceSER.queryResourceRightsByRoleID(role
				.getId());
		List<Resource> resourcesTree = resourceSER.queryFuncResourcesTree();
		for (Resource resource : resourcesTree) {
			resource.setChecked(RightsHelper.testRights(
					RightsHelper.sumRights(rightsID), resource.getId()));
		}
		return rt.menuList(resourcesTree);
	}

	// 为角色保存已经修改的权限
	@RequestMapping(value = "/saveAuthByRole", method = { RequestMethod.POST })
	public @ResponseBody
	boolean saveAuthByRole(Integer[] menuRights, Integer[] funcRights,Integer roleID)
			throws Exception {
		Integer[] rights = new Integer[menuRights.length+funcRights.length];
		for (int i = 0;i< menuRights.length; i++) {
			rights[i] = menuRights[i];
		}
		for (int i = 0;i< funcRights.length; i++) {
			rights[menuRights.length+i] = funcRights[i];
		}
		resourceSER.updateRightsResourceByRole(rights,roleID);
		return false;
	}
}
